package com.hinner.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hinner.entity.Chinas;
import com.hinner.entity.dto.RegionDTO;
import com.hinner.entity.fenbushi.FbsLineMain;
import com.hinner.entity.fenbushi.dto.JgdwDTO;
import com.hinner.entity.fenbushi.vo.JgdwVO;
import com.hinner.exception.CustomException;
import com.hinner.mapper.ChinasMapper;
import com.hinner.mapper.FbsLineMainMapper;
import com.hinner.service.ChinasService;
import com.hinner.service.FbsLineMainService;
import com.hinner.utils.RandomIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class FbsLineMainServiceImpl extends ServiceImpl<FbsLineMainMapper, FbsLineMain> implements FbsLineMainService {
    private static final Logger log = LoggerFactory.getLogger(FbsLineMainServiceImpl.class);

    @Override
    public List<FbsLineMain> getAllLineByJgdwId(String jgdwId) {
        LambdaQueryWrapper<FbsLineMain> wr = new LambdaQueryWrapper<FbsLineMain>().eq(StringUtils.isNotEmpty(jgdwId), FbsLineMain::getJgdwId, jgdwId);
        return this.list(wr);
    }

    @Override
    public List<JgdwVO> getAllJgdwByPc(String provinceCode) {
        LambdaQueryWrapper<FbsLineMain> wr = new LambdaQueryWrapper<FbsLineMain>().eq(StringUtils.isNotEmpty(provinceCode), FbsLineMain::getProvinceCode, provinceCode);
        List<FbsLineMain> list = this.list(wr);
        if (CollectionUtils.isEmpty(list)) return null;
        // 基于监管单位名称去重用 Collectors.toMap()，不能用distinct()
        Map<Long, FbsLineMain> map = list.stream().collect(Collectors.toMap(
                FbsLineMain::getJgdwId,
                fbsLineMain -> fbsLineMain,
                (existing, replacement) -> existing
        ));
        List<FbsLineMain> distincts = new ArrayList<>(map.values());
        List<JgdwVO> vos = new ArrayList<>();
        for (FbsLineMain distinct : distincts) {
            JgdwVO vo = new JgdwVO();
            vo.setId(distinct.getId().toString());
            vo.setProvinceCode(distinct.getProvinceCode());
            vo.setJgdwName(distinct.getJgdwName());
            vo.setJgdwId(distinct.getJgdwId());
            vo.setProvince(distinct.getProvince());
            vos.add(vo);
        }
        return vos;
    }

    /**
     * 新增监管单位
     * @param dto
     */
    @Override
    public void addJgdw(JgdwDTO dto) {
        System.out.println(dto);
        FbsLineMain fbsLineMain = new FbsLineMain();
        fbsLineMain.setJgdwName(dto.getJgdwName());
        fbsLineMain.setJgdwId(Long.parseLong(RandomIdGenerator.generateRandomId())); //最好还是随机比较好
        fbsLineMain.setCity(dto.getCity());
        fbsLineMain.setProvince(dto.getProvince());
        fbsLineMain.setProvinceCode(dto.getProvinceCode());
        fbsLineMain.setJgdwDesc(dto.getJgdwDesc());
        this.saveOrUpdate(fbsLineMain);
    }
}
